import re
import subprocess

from PySide6.QtCore import Slot
from qfluentwidgets import PushButton, SearchLineEdit

from app.common.translator import Translator
from app.utils.log_rec import logger
from app.view.interfaces import InterfacesView


class InterfaceEmulatorLaunchTool(InterfacesView):

    def __init__(self, parent=None):
        self.translator = Translator()
        super().__init__(
            title=self.translator.basicInput,
            subtitle="scenario goal simulator",
            parent=parent,
        )
        self.setObjectName(self.__class__.__name__)
        self.translator = Translator()

        self.button_check_java = PushButton(self.translator.Check)
        self.button_check_java.clicked.connect(self.show_java_msg)
        self.card_java_env = self.add_card(
            self.translator.CheckJavaENV, self.button_check_java
        )

        self.airport = SearchLineEdit()

    @Slot()
    def show_java_msg(self):
        """
        展示JAVA信息
        :return:
        """
        try:
            sto = subprocess.run(
                ["java", "-version"], capture_output=True, text=True, check=True
            )
            logger.debug(sto)
            if sto:
                logger.debug(f"java msg: {sto}")
                __java_msg: dict = {}
                version_pattern = re.compile(r'java version "([^"]+)"')
                __java_msg["version"] = version_pattern.search(sto.stderr).group(1)
                self.add_label_to_card(__java_msg)
            else:
                logger.error(f"java msg: {sto}")
        except subprocess.CalledProcessError:
            msg = "Java is not installed."
            self.add_label_to_card({"Error": msg})
            logger.error(msg)

    def select_airport(self):
        """

        :return:
        """
